/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MyLogger.java
 *
 * Created on 5 avr. 2013, 00:06:06
 */
package my.monitor;

import java.beans.PropertyChangeEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import my.monitor.controller.MyController;
import my.monitor.model.Message;

/**
 *
 * @author Piro
 */
public class MyLoggerPanel extends AbstractViewPanel {

    private MyController controller;
    private String logs = "";
    private int nbMessage = 0;
    static private int nbMessageMax = 9;

    /** Creates new form MyLogger */
    public MyLoggerPanel() {
        initComponents();
        jTextAreaLog.setEditable(false);
    }

    public MyLoggerPanel(MyController controller) {
        initComponents();
        this.controller = controller;
        jTextAreaLog.setEditable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaLog = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(410, 230));
        setMinimumSize(new java.awt.Dimension(410, 230));
        setPreferredSize(new java.awt.Dimension(410, 230));

        jTextAreaLog.setColumns(20);
        jTextAreaLog.setRows(5);
        jScrollPane1.setViewportView(jTextAreaLog);

        jLabel1.setText("Log des 10 derniers messages reçus et envoyés");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
                    .add(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 184, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaLog;
    // End of variables declaration//GEN-END:variables

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                MyController.MESSAGE)) {

            Message message = (Message) evt.getNewValue();
            Message oldmessage = (Message) evt.getOldValue();
            
            if (message == null) {
                nbMessage = 0;
                logs = "";
                //jTextAreaLog.setText("");
            } else {
                if (nbMessage > nbMessageMax) {
                    // Supprime la première ligne
                    logs = logs.substring(logs.indexOf("\n")+1);
                } else {
                    nbMessage ++;
                }
                DateFormat checkMilliFormat = new SimpleDateFormat("mm:ss:SSS");
                System.out.println(checkMilliFormat.format(message.getDate()));
                logs += checkMilliFormat.format(message.getDate())+ " "; 
                if (message.getSens() == Message.reception) { //message reception
                    logs += "<< " + message.getType() + message.getName() + "\n";
                    //jTextAreaLog.append("<--- " + message.getType() + "\n");
                } else {
                    /*if (oldmessage != null) {
                        if (message.getType().equals(oldmessage.getType())) {
                            jTextAreaLog.append(".");
                        } else {
                            jTextAreaLog.append("\n" + message.getType());
                        }
                    } else {
                        jTextAreaLog.append(message.getType());
                    }*/
                    logs += ">> " + message.getType() + message.getName() + "\n";
                     //jTextAreaLog.append(message.getType() + " --->\n");
                }
            }
            jTextAreaLog.setText(logs);
        }
    }

    public void delete() {
        jTextAreaLog.setText("");
    }
}
